package com.tfjybj.itoo.exam.provider.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.dmsdbj.itoo.tool.base.service.impl.BaseServicePlusImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.ExamClassifyEntity;
import com.tfjybj.itoo.exam.model.ExamClassifyTreeModel;
import com.tfjybj.itoo.exam.provider.dao.ExamClassifyDao;
import com.tfjybj.itoo.exam.provider.service.ExamClassifyService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ExamClassifyService接口实现类
 * ${base}表
 *
 * @author 任晓帅
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 19:40:28
 */
@Service("examClassifyService")
public class ExamClassifyServiceImpl extends BaseServicePlusImpl<ExamClassifyDao, ExamClassifyEntity> implements ExamClassifyService {
	
	//region 模板生成
    @Resource
    private ExamClassifyDao examClassifyDao;

	 @Override
    public PageInfo<ExamClassifyEntity> queryByLikeName(String name, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(examClassifyDao.queryLikeName(name));
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */


    @Override
    public List<ExamClassifyTreeModel> queryExamClassifyAll(String operatorId) {
        List<ExamClassifyEntity> examClassList;
        examClassList = examClassifyDao.queryExamClassifyAll(operatorId);
        if (examClassList.size() == 0) {
            ExamClassifyEntity examClassifyEntity = new ExamClassifyEntity();
            examClassifyEntity.setName("廊坊师范学院")
                    .setParentId("0")
                    .setIsLeaf(0)
                    .setOperator(operatorId);
            if (this.save(examClassifyEntity)) {
                examClassList = examClassifyDao.queryExamClassifyAll(operatorId);
            } else {
                return null;
            }
        }
        List<ExamClassifyTreeModel> examTreeList=new ArrayList<>();
        for (ExamClassifyEntity examcla:examClassList) {
            ExamClassifyTreeModel examTree=new ExamClassifyTreeModel();
            examTree.setId(examcla.getId());
            examTree.setName(examcla.getName());
            examTree.setParentId(examcla.getParentId());
            examTree.setOpen("0".equals(examcla.getParentId()));
            examTreeList.add(examTree);
        }
        return examTreeList;
    }

    //    添加事件
    @Override
    public String saveExamCalssify(ExamClassifyTreeModel examTreeModel) {
        String examClassify= IdWorker.getIdStr();
        ExamClassifyEntity examIfy = new ExamClassifyEntity();
        examIfy.setId(examClassify);
        examIfy.setParentId(examTreeModel.getParentId());
        examIfy.setName(examTreeModel.getName());
        examIfy.setOperator(examTreeModel.getOperator());
        return this.save(examIfy) ? examClassify :null;
    }


    //    编辑事件
    @Override
    public int updateExamCalssify(ExamClassifyTreeModel examTreeModel) {
        ExamClassifyEntity examClassifyEntity=new ExamClassifyEntity();
        examClassifyEntity.setName(examTreeModel.getName());
        examClassifyEntity.setId(examTreeModel.getId());
        examClassifyEntity.setParentId(examTreeModel.getParentId());
        examClassifyEntity.setOperator(examTreeModel.getOperator());
        return this.updateById(examClassifyEntity)?1:0;
    }

    @Override
    public int treeDelete(String treeId) {
        return examClassifyDao.treeDelete(treeId);
    }


}
